/*
  © 2022 BLACK SESAME TECHNOLOGIES
 All rights reserved.
 (This product is licensed to: [Hanhai-ADSP Autonomous Driving Solution Platform]
 This computer software is protected by copyright law and international treaties.
 Use, reproduction, or distribution of this software is subject to the license agreement between you
 and Black Sesame Technologies. Unauthorized reproduction or distribution of this program, or any
 portion of it, may result in severe civil and criminal penalties and will be prosecuted to the
 maximum extent possible under the law.
*/

#ifndef _DAL_REF_COUNT_HPP_
#define _DAL_REF_COUNT_HPP_

#include <atomic>

class ReferenceCount
{
public:
    void Decrease()
    {
        uint32_t current_reference_count = reference_count_.load();
        do {
            if (current_reference_count == 0)
                return;
        } while (!reference_count_.compare_exchange_strong(current_reference_count,
                                                           current_reference_count - 1));
    }
    uint32_t Get()
    {
        return reference_count_.load();
    }
    void Increase()
    {
        reference_count_.fetch_add(1);
    }

private:
    std::atomic<uint32_t> reference_count_ = { };
};

#endif  // _DAL_REF_COUNT_HPP_